﻿using Growatt.WaveData.VirtualOscilloscope;
using System;
using System.Windows.Forms;

namespace Growatt.Component.Frm
{
    /// <summary>
    /// 输入对话框
    /// </summary>
    public partial class FrmInputBox :Form
    {
        private static FrmInputBox frmInputBox = null;

        /// <summary>
        ///显示输入对话框,返回DialogResult OK Cancel
        /// </summary>
        /// <param name="strMessage">显示消息</param>
        /// <param name="isShowBtn">是否显示按钮</param>
        public static DialogResult ShowInputDialog(string strMessage)
        {
            if (frmInputBox == null)
                frmInputBox = new FrmInputBox();
            frmInputBox.lab_title.Text = strMessage;
            return frmInputBox.ShowDialog();
        }

        private static Func<string, string> Func;
        private static Action<bool, (string, string, string, string)>  FuncAddCallback;

        /// <summary>
        ///显示输入对话框,返回Bool
        /// </summary>
        /// <param name="strMessage">显示消息</param>
        /// <param name="isShowBtn">是否显示按钮</param>
        public static bool ShowInputBox(string strMessage, ref string inputvalue, string initValue = null)
        {
            if (frmInputBox == null)
                frmInputBox = new FrmInputBox();
            frmInputBox.lab_title.Text = strMessage;
            frmInputBox.txt_inputValue.Text = inputvalue;
            if (initValue != null)
            {
                frmInputBox.InputValue = initValue;
            }
            DialogResult dialogResult = frmInputBox.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                inputvalue = frmInputBox.txt_inputValue.Text.Trim();
                frmInputBox.lb_tip = null;
                return true;
            }
            else
            {
                inputvalue = null;
                frmInputBox.lb_tip = null;
                return false;
            }
        }

        /// <summary>
        ///显示输入对话框,返回Bool
        /// </summary>
        /// <param name="strMessage">显示消息</param>
        /// <param name="isShowBtn">是否显示按钮</param>
        public static bool ShowInputTipBox(string strMessage, ref string inputvalue, Func<string, string> func = null, Action<bool,(string,string,string,string)> AddCallback = null,string initValue = null)
        {
            if (frmInputBox == null)
                frmInputBox = new FrmInputBox();
            frmInputBox.comboBox_Type.SelectedIndex = 0;
            frmInputBox.comboBox_Scale.SelectedIndex = 0;
            frmInputBox.lab_title.Text = strMessage;
            frmInputBox.txt_inputValue.Text = inputvalue;
            Func = func;
            FuncAddCallback = AddCallback;
            if (initValue != null)
            {
                frmInputBox.InputValue = initValue;
            }
            DialogResult dialogResult = frmInputBox.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                inputvalue = frmInputBox.txt_inputValue.Text.Trim();
                return true;
            }
            else
            {
                inputvalue = null;
                return false;
            }
        }

        /// <summary>
        /// 是否是输入文件名称,是则需要受到文件名称格式限制
        /// </summary>
        public bool isFileName = true;

        /// <summary>
        /// 输入对话框
        /// </summary>
        public FrmInputBox()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 输入值
        /// </summary>
        public string InputValue
        {
            get; set;
        }

        /// <summary>
        /// 确定按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            InputValue = txt_inputValue.Text.Trim();
            string tipstr = Func?.Invoke(InputValue);
            if (tipstr != null||string.IsNullOrEmpty(InputValue))
            {
                if (lb_tip != null)
                    lb_tip.Text = tipstr;
            }
            else /*返回Null时表示正确*/
            {
                if (lb_tip != null)
                    lb_tip.Text = null;
                this.DialogResult = DialogResult.OK;

                WaveDataHelpWin.ShowMessage("添加成功!");
                FuncAddCallback?.Invoke(true,(txt_inputValue.Text, comboBox_Type.Text, comboBox_Scale.Text,textBox1.Text));
                this.Close();
            }
        }

        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 载入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmInputBox_Load(object sender, EventArgs e)
        {
            txt_inputValue.Focus(); //输入框获取焦点
                                    //     txt_inputValue.Text = InputValue;//设置默认值
        }

        /// <summary>
        /// 输入框键盘事件 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_inputValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (isFileName) //是文件名称就需要受到格式限制
                if (e.KeyChar == ' ' || e.KeyChar == '@' || e.KeyChar == '/' || Convert.ToInt32(e.KeyChar) == 92 || e.KeyChar == '.')
                {
                    e.Handled = true;
                }
                else
                {
                    e.Handled = false;
                }
        }
    }
}